package cn.wyl.basic.service.impl;

import cn.wyl.basic.mapper.BaseMapper;
import cn.wyl.basic.query.BaseQuery;
import cn.wyl.basic.service.BaseService;
import cn.wyl.basic.util.PageList;
import org.apache.rocketmq.client.exception.MQClientException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
//BaseMapper<T>不能交给spring管理，所以不能加service注解，可以加事务注解
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements BaseService<T> {
    //只能写Autowired，@Autowired spring根据类型匹配 @Resource根据名字匹配
    @Autowired
    private BaseMapper<T> baseMapper;

    @Override
    public T loadById(Long id) {
        return baseMapper.loadById(id);
    }

    @Override
    public List<T> loadAll() {
        return baseMapper.loadAll();
    }

    @Transactional
    @Override
    public void save(T t) throws Exception {
        baseMapper.save(t);
    }

    @Transactional
    @Override
    public void update(T t) {
        baseMapper.update(t);
    }
    @Transactional
    @Override
    public void remove(Long id) {
        baseMapper.remove(id);
    }

    @Override
    public PageList<T> queryPage(BaseQuery query) {
        Integer totals = baseMapper.queryCount(query);
        if(totals == null || totals < 1){
            return new PageList<T>();
        }

        List<T> data = baseMapper.queryData(query);
        return new PageList<T>(totals,data);
    }
    @Transactional
    @Override
    public void patchDel(List<Long> ids) {
        baseMapper.patchDel(ids);
    }
}
